package cn.lsh.writable;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableUtils;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class FriendRelation implements WritableComparable<FriendRelation> {
	private String from;
	private String to;
	private int n;

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public int getN() {
		return n;
	}

	public void setN(int n) {
		this.n = n;
	}

	@Override
	public int compareTo(FriendRelation o) {
		int i = this.from.compareTo(o.getFrom());
		if (i == 0) {
			i = this.to.compareTo(o.getTo());
			if (i == 0) {
				return Integer.compare(this.n, o.getN());
			}
			return i;
		}
		return i;
	}

	@Override
	public void write(DataOutput dataOutput) throws IOException {
		//这类序列话的类型要和下面反序列化的顺序一致
		dataOutput.writeUTF(this.from);
		dataOutput.writeUTF(this.to);
		dataOutput.writeInt(this.n);
	}

	@Override
	public void readFields(DataInput dataInput) throws IOException {
		this.from = dataInput.readUTF();
		this.to = dataInput.readUTF();
		this.n = dataInput.readInt();
	}
}
